<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
//使用类
use App\Http\Controllers\CodeController;
//使用类
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Hash;

class LoginController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   //加载页面


        return view("admin.login");
    }



      /**
     * 处理登录
     */

    public function check(Request $request)
    {

        $ucode=$request->input('code');
        $code  = session('code');
        $password=$request->input('password');
        $nickname=$request->input('nickname');


          $rules=[
            'user' => 'required',
            'code' => 'required',
            'password' => 'required',
        ];
        // 表单验证的提示信息

        $message=[

            "user.required"=>"请输入用户名",
            "code.required"=>"请输入验证码",
            "password.required"=>"请输入密码",

        ];

        // 使用laravel的表单验证
        $this->validate($request,$rules,$message);

        // 检测验证码
        if ($ucode==$code) {

            // 验证密码
            $data=\DB::table('admin')->where('nickname','=',"$nickname")->where('statu','=','0')->first();
            //$data=\DB::select("select * from admin where nickname='$nickname' and statu= 0");

            if ($data) {
            	$hashedPassword=$data['password'];
               if (Hash::check($password,$hashedPassword)) {

               		  // 声明数组

                    $arr=[];

                    $arr['lasttime']=time();
                    $arr['count']=++$data['count'];
                    // 更新登录信息

                    \DB::table('admin')->where('id',$data['id'])->update($arr);
                    // 存session
                    session(['szyLy.nickname'=>$data['nickname']]);
                    session(['szyLy.id'=>$data['id']]);
                    session(['szyLy.lasttime'=>$data['lasttime']]);
                    session(['szyLy.count'=>$data['count']]);

                    if($data['authority']==1){
                    	 session(['szyLy.authority'=>$data['authority']]);
                    }
            return


                    // 跳转到首页
                    return redirect('admin');
    			}else{
    				return back()->with("error",'密码错误');
    			}
  			}else{
  				return back()->with("error",'用户名不存在');
  			}
		}else{back()->with("error",'验证码错误');
        }
		return redirect("admin");
	}


	/**
     * 退出登录
     */

	 public function loginout(Request $request)
	    {
	        $request->session()->flush();

	        return redirect('admin/login');
	    }

}
